package mo.offer_special;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class L033 {

    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> res = new HashMap<>();
        for (String str : strs) {
            int[] count = new int[26];
            for (char c : str.toCharArray()) {
                count[c-'a']++;
            }
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < 26; i++) {
                if (count[i] > 0) {
                    builder.append((char)('a' + i));
                    builder.append(count[i]);
                }
            }
            String key = builder.toString();
            List<String> group = res.getOrDefault(key, new ArrayList<>());
            group.add(str);
            res.put(key, group);
        }
        return new ArrayList<>(res.values());
    }

}
